package com.nhim.cms.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.stereotype.Repository;

import com.nhim.cms.domain.Template;
import com.nhim.core.AppContext;
import com.nhim.core.dao.CoreDAO;

@Repository
public class TemplateDAO extends CoreDAO<Integer, Template> implements ITemplateDAO {
	
	
		// property constants
		public static final String NAME = "name";
		public static final String GROUP_TEMPLATE_ID = "groupTemplateId";
		public static final String SITE_TEMPLATE_ID = "siteTemplateId";
		public static final String IS_DEFAULT = "isDefault";
		public static final String TYPE = "type";
	

		@Autowired
		EntityManagerFactory entityManagerFactory;

		@PostConstruct
		public void init() {
			super.setEntityManagerFactory(entityManagerFactory);
		}

		
		public static ITemplateDAO getFromApplicationContext(ApplicationContext ctx) {
			return (ITemplateDAO) ctx.getBean(TemplateDAO.class);
		}
		public static ITemplateDAO getInstance()
		{
			return getFromApplicationContext(AppContext.getApplicationContext());
		}


		@SuppressWarnings("unchecked")
		@Override
		public List<Template> findByProperty(String propertyName, final Object value) {
			try {
				final String queryString = "select model from Template model where model."
						+ propertyName + "= :propertyValue";
				return getJpaTemplate().executeFind(new JpaCallback<Object>() {
					public Object doInJpa(EntityManager em)
							throws PersistenceException {
						Query query = em.createQuery(queryString);
						query.setParameter("propertyValue", value);
						return query.getResultList();
					}
				});
			}
			catch(org.springframework.dao.EmptyResultDataAccessException e)
			{
				return new ArrayList<Template>();
			}
			catch (RuntimeException re) {
				throw re;
			}

		}


		@Override
		public Template findGroupTemplateByName(final String name) {
			try {
				final String queryString = "select model from Template model where model.type = 1 and model.name= :propertyValue";
				return (Template) getJpaTemplate().execute(new JpaCallback<Object>() {
					public Object doInJpa(EntityManager em)
							throws PersistenceException {
						Query query = em.createQuery(queryString);
						query.setParameter("propertyValue", name);
						return query.getSingleResult();
					}
				});
			}
			catch(org.springframework.dao.EmptyResultDataAccessException e)
			{
				return null;
			}
			catch (RuntimeException re) {
				re.printStackTrace();
				return null;
			}

		}


		public Template findSiteTemplateByName(final String name) {
			try {
				final String queryString = "select model from Template model where model.type = 0 and model.name= :propertyValue";
				return (Template) getJpaTemplate().execute(new JpaCallback<Object>() {
					public Object doInJpa(EntityManager em)
							throws PersistenceException {
						Query query = em.createQuery(queryString);
						query.setParameter("propertyValue", name);
						return query.getSingleResult();
					}
				});
			}
			catch(org.springframework.dao.EmptyResultDataAccessException e)
			{
				return null;
			}
			catch (RuntimeException re) {
			re.printStackTrace();
			return null;
			}

		}
		public Template findContentTemplateByName(final Integer contentId,final String name) {
			try {
				final String queryString = "select model from Template model join model.contentType ct where model.type = 2 and ct.id = :contentTypeId  and model.name= :propertyValue";
				return (Template) getJpaTemplate().execute(new JpaCallback<Object>() {
					public Object doInJpa(EntityManager em)
							throws PersistenceException {
						Query query = em.createQuery(queryString);
						query.setParameter("propertyValue", name);
						query.setParameter("contentTypeId", contentId);
						return query.getSingleResult();
					}
				});
			}
			catch(org.springframework.dao.EmptyResultDataAccessException e)
			{
				return null;
			}
			catch (RuntimeException re) {
				re.printStackTrace();
				return null;
			}

		}


		@Override
		public List<Template> findAllGroupTemplate() {
			return findByProperty(TYPE,1);
		}
		
		@Override
		public List<Template> findAllSiteTemplate() {
			return findByProperty(TYPE,0);
		}


		@SuppressWarnings("unchecked")
		@Override
		public List<Template> findContentTemplateById(final Integer id) {
			try {
				final String queryString = "select model from Template model join model.contentType ct where model.type = 2 and ct.id = :contentTypeId";
				return (List<Template>) getJpaTemplate().executeFind(new JpaCallback<Object>() {
					public Object doInJpa(EntityManager em)
							throws PersistenceException {
						Query query = em.createQuery(queryString);
						query.setParameter("contentTypeId", id);
						return query.getResultList();
					}
				});
			} 
			catch(org.springframework.dao.EmptyResultDataAccessException e)
			{
				return new ArrayList<Template>();
			}
			catch (RuntimeException re) {
				re.printStackTrace();
				return null;
			}
		}


		

}
